﻿using System;
using System.IO;
using Microsoft.Practices.Prism.ViewModel;



namespace ScriptEase
{
    /// <summary>
    /// A local workspace file to WebResource mapping.
    /// </summary>
    public class MappingModel : NotificationObject
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MappingModel" /> class.
        /// </summary>
        /// <param name="webResource">The web resource.</param>
        /// <param name="fullPath">The full path.</param>
        /// <param name="fileRelativePath">An absoulte or project relative path to the script file.</param>
        /// <remarks>
        /// The MappingModel is initialized with all the information from the WebResource.
        /// No file or filename exists yet. Synchronization is set to disbaled by default.
        /// </remarks>
        internal MappingModel(WebResource webResource, string fullPath, string fileRelativePath)
        {
            this.Id = Guid.NewGuid();
            this.WebResourceId = webResource.Id;
            this.WebResourceType = webResource.GetTypeValue();
            this.WebResourceName = webResource.Name;
            this.FileRelativePath = fileRelativePath;
            this.AutoUpdate = false;
            this.IsManaged = webResource.IsManaged.GetValueOrDefault();
            this.IsCustomizable = webResource.IsCustomizable.Value;

            if (File.Exists(fullPath))
            {
                this.SyncStatus = FileSyncStatus.FileNotInCrm;
                this.FileInfo = new FileInfo(fullPath);
            }
            else
            {
                this.SyncStatus = FileSyncStatus.FileNotDownloaded;
                this.FileInfo = null;
            }
        }



        public Guid WebResourceId    {  get; private set; }


        public Guid Id { get; private set; }


        /// <summary>
        /// The WebResource realtive file-path to the WebResource base directory
        /// or the absoute path if the WebResource is not located under the WebResource base directory.
        /// </summary>
        public string FileRelativePath
        {
            get { return _fileRelativePath; }
            set
            {
                if (_fileRelativePath == value) return;
                _fileRelativePath = value;
                base.RaisePropertyChanged(() => this.FileRelativePath);
            }
        }

        private string _fileRelativePath;


        /// <summary>
        /// Gets or sets a value indicating whether CRM should be updated,
        /// when the files has changed.
        /// </summary>
        public bool AutoUpdate
        {
            get { return _autoUpdate; }
            set
            {
                if (_autoUpdate == value) return;
                _autoUpdate = value;
                base.RaisePropertyChanged(() => this.AutoUpdate);
            }
        }

        private bool _autoUpdate;


        public FileSyncStatus SyncStatus
        {
            get { return _syncStatus; }
            set
            {
                if (_syncStatus == value) return;
                _syncStatus = value;
                base.RaisePropertyChanged(() => this.SyncStatus);
            }
        }

        private FileSyncStatus _syncStatus;


        public FileInfo FileInfo
        {
            get { return _fileInfo; }
            set
            {
                if (_fileInfo == value) return;
                _fileInfo = value;
                base.RaisePropertyChanged(() => this.FileInfo);
            }
        }

        private FileInfo _fileInfo;


        // The following properties do not change after construction anymore
        // and there no need to raise change events
        public string WebResourceName { get; private set; }
        private int WebResourceType { get; set; }
        public bool IsManaged { get; private set; }
        public bool IsCustomizable { get; private set; }


        /// <summary>
        /// A flag that is used internally to ignore the next file change event.
        /// </summary>
        internal bool IgnoreNextFileChange { get; set; }
    }
}